package org.jks.service;

import java.util.ArrayList;
import java.util.List;

import org.jks.model.Device;
import org.jks.model.DeviceType;
import org.springframework.stereotype.Service;

@Service
public class DeviceService extends EntityService<Device>{
	
	public DeviceService(){
		MODEL_NAME = Device.class.getSimpleName();
		CLASS_NAME = Device.class;
	}
	
	/**
	 * 统计某设备类型的数量
	 * 条件：
	 * 1、备勤状态
	 * @param type 设备类型
	 * @return 返回设备数
	 */
	public long countByType(DeviceType type) {
		if(type == null){
			return 0;
		}
		return count("select count(*) from Device where Type.Id="+type.getId() + " and CurrentStatus.Id=1");
	}
	
	/**
	 * 统计某设备类型的数量
	 * 条件：
	 * 1、备品
	 * 2、备勤状态
	 * @param type 设备类型
	 * @return 返回设备数
	 */
	public long countByTypeForSparePart(DeviceType type){
		return count("select count(*) from Device where Type.Id="+type.getId() + " and CurrentStatus.Id=1 and IsSparePart=1");
	}
	
	public List<Device> distinct(){
		return find("from Device group by Type");
	}
	
	public List<Device> listAllByType(long statusId){
		return find("from Device where CurrentStatus.Id=" + statusId);
	}
	
	public List<Device> listByType(long id, long statusId){
		return find("from Device where Type.Id="+id + " and CurrentStatus.Id=" + statusId);
	}
	
	public List<Device> listByType(long id){
		return find("from Device where Type.Id="+id);
	}
	
	public Device findByTdCode(String tdCode){
		String hql = "from Device where TdCode=?";
		List<Object> params = new ArrayList<Object>();
		params.add(tdCode);
		return get(hql, params);
	}
}

